package com.mf.web.authority;

import com.mf.common.sso.LoginContext;
import com.mf.domain.authority.RoleInfo;
import com.mf.domain.authority.UserInfo;
import com.mf.domain.authority.UserRoleRelation;
import com.mf.domain.general.PageBean;
import com.mf.service.authority.RoleInfoService;
import com.mf.service.authority.UserInfoService;
import com.mf.service.authority.UserRoleRelationService;
import com.mf.web.framework.advice.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户角色关联信息Controller
 * User: zhaoming
 * DateTime: 2016-07-15
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/web/userRoleRelation")
public class UserRoleRelationController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private UserRoleRelationService userRoleRelationService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private RoleInfoService roleInfoService;

    /**
     * 分配角色页面
     * @param userInfo 用户信息Domain
     * @return 模型视图
     */
    @RequestMapping(value = "/assignRolePage", method = RequestMethod.POST)
    public ModelAndView assignRolePage(UserInfo userInfo) {
        ModelAndView modelAndView = new ModelAndView("authority/user/user_assign_role");
        modelAndView.addObject("userInfo", userInfoService.selectByPrimaryKey(userInfo));
        return modelAndView;
    }

    /**
     * 分配用户页面
     * @param roleInfo 角色信息Domain
     * @return 模型视图
     */
    @RequestMapping(value = "/assignUserPage", method = RequestMethod.POST)
    public ModelAndView assignUserPage(RoleInfo roleInfo) {
        ModelAndView modelAndView = new ModelAndView("authority/role/role_assign_user");
        modelAndView.addObject("roleInfo", roleInfoService.selectByPrimaryKey(roleInfo));
        return modelAndView;
    }

    /**
     * 新增用户角色关联信息
     * @param userRoleRelation 用户角色关联信息Domain
     * @return 执行结果
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean insert(UserRoleRelation userRoleRelation) {
        boolean flag = userRoleRelationService.insert(userRoleRelation, LoginContext.getLoginUser().getUserCode());
        if(!flag){
            throw new BusinessException("新增失败");
        }
        return flag;
    }

    /**
     * 根据主键维护用户角色关联信息
     * @param userRoleRelation 用户角色关联信息Domain
     * @return 执行结果
     */
    @RequestMapping(value = "/updateByPrimaryKey", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean updateByPrimaryKey(UserRoleRelation userRoleRelation) {
        boolean flag = userRoleRelationService.updateByPrimaryKey(userRoleRelation, LoginContext.getLoginUser().getUserCode());
        if(!flag){
            throw new BusinessException("维护失败");
        }
        return flag;
    }

    /**
     * 根据主键删除用户角色关联信息 (物理删除)
     * @param userRoleRelation 用户角色关联信息Domain
     * @return 执行结果
     */
    @RequestMapping(value = "/deleteByPrimaryKey", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean deleteByPrimaryKey(UserRoleRelation userRoleRelation){
        boolean flag = userRoleRelationService.deleteByPrimaryKey(userRoleRelation);
        if(!flag){
            throw new BusinessException("删除用户角色关联信息失败");
        }
        return flag;
    }

    /**
     * 根据主键删除用户角色关联信息 (逻辑删除)
     * @param userRoleRelation 用户角色关联信息Domain
     * @return 执行结果
     */
    @RequestMapping(value = "/deleteByPrimaryKeyLogically", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean deleteByPrimaryKeyLogically(UserRoleRelation userRoleRelation){
        boolean flag = userRoleRelationService.deleteByPrimaryKeyLogically(userRoleRelation, LoginContext.getLoginUser().getUserCode());
        if(!flag){
            throw new BusinessException("删除用户角色关联信息失败");
        }
        return flag;
    }

    /**
     * 根据主键查询用户角色关联信息
     * @param userRoleRelation 用户角色关联信息Domain
     * @return 用户角色关联信息
     */
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public UserRoleRelation selectByPrimaryKey(UserRoleRelation userRoleRelation) {
        return userRoleRelationService.selectByPrimaryKey(userRoleRelation);
    }

    /**
     * 根据条件查询用户角色关联信息
     * @param userRoleRelation 用户角色关联信息Domain
     * @return 用户角色关联信息列表
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<UserRoleRelation> selectByCondition(UserRoleRelation userRoleRelation) {
        return userRoleRelationService.selectByCondition(userRoleRelation);
    }

    /**
     * 分页查询用户角色关联信息
     * @param userRoleRelation 用户角色关联信息Domain
     * @param page 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 用户角色关联信息列表
     */
    @RequestMapping(value = "/pageQuery", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> pageQuery(UserRoleRelation userRoleRelation,
                                    @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                    @RequestParam(value = "rows", required = false, defaultValue = "10") int pageSize,
                                    @RequestParam(value = "sidx") String sortName,
                                    @RequestParam(value = "sord") String sortOrder) {

        PageBean<UserRoleRelation> pageBean = new PageBean<>(page, pageSize);
        LinkedHashMap<String, String> sortItemMap = new LinkedHashMap<>();
        sortItemMap.put(sortName, sortOrder);
        pageBean.setSortItemMap(sortItemMap);
        pageBean = userRoleRelationService.pageQuery(pageBean, userRoleRelation);

        Map<String, Object> result = new HashMap<>();
        result.put("root", pageBean.getUnderly());
        result.put("page", pageBean.getPage());
        result.put("total", pageBean.getPageCount());
        result.put("records", pageBean.getItemCount());
        return result;
    }

    /**
     * 检查用户的角色重复数据
     * @param assignUserCode 用户编码
     * @param roleCode 角色编码
     * @return 验证结果
     */
    @RequestMapping(value = "/checkRepeat", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> checkRepeat(String assignUserCode, String roleCode) {
        UserRoleRelation userRoleRelation = new UserRoleRelation();
        userRoleRelation.setUserCode(assignUserCode);
        userRoleRelation.setRoleCode(roleCode);
        return userRoleRelationService.checkRepeat(userRoleRelation);
    }

    /**
     * 验证用户编码
     * @param userCode 用户编码
     * @param assignRoleCode 角色编码
     * @return 验证结果
     */
    @RequestMapping(value = "/checkUserCode", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> checkUserCode(@RequestParam String userCode, @RequestParam String assignRoleCode) {
        return userRoleRelationService.checkUserCode(userCode, assignRoleCode);
    }

    /**
     * 获取角色信息
     * @param userRoleRelation 用户角色关联信息Domain
     */
    @RequestMapping(value = "/getRoles", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<Map<String, Object>> getRoles(UserRoleRelation userRoleRelation) {
        return userRoleRelationService.getRoles(userRoleRelation);
    }

    /**
     * 分配角色
     * @param userCode 用户编码
     * @param roleCode 角色编码
     */
    @RequestMapping(value = "/assignRole", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean assignRole(String userCode, String roleCode) {
        boolean flag = userRoleRelationService.assignRole(userCode, roleCode);
        if(!flag){
            throw new BusinessException("分配角色失败");
        }
        return flag;
    }

    /**
     * 获取用户信息
     * @param userRoleRelation 用户角色关联信息Domain
     */
    @RequestMapping(value = "/getUsers", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<Map<String, Object>> getUsers(UserRoleRelation userRoleRelation){
        return userRoleRelationService.getUsers(userRoleRelation);
    }

    /**
     * 分配用户
     * @param userCode 用户编码
     * @param roleCode 角色编码
     */
    @RequestMapping(value = "/assignUser", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean assignUser(String userCode, String roleCode) {
        boolean flag = userRoleRelationService.assignUser(userCode, roleCode);
        if(!flag){
            throw new BusinessException("分配用户失败");
        }
        return flag;
    }
}